#include "ejconfig.h"
#include <QFile>
#include <QStack>
#include "ejconfigmanager.h"

EJConfig::~EJConfig()
{

}

bool EJConfig::open(const QString &name)
{
    auto file = new QFile(name);
    if(!file->open(QFile::ReadOnly | QFile::Text))
    {
        return false;
    }
    m_file = name;
    m_xml.setDevice(file);

    parse();

    return true;
}

QMap<QString, QString>& EJConfig::getAttr(const QStringList &lable)
{
    if (m_moudle.child.empty() || lable.empty()) return m_moudle.attr;

    auto l = lable;
    EJModule *mp = &m_moudle;

    while(!mp->child.empty())
    {
        for (auto it = mp->child.begin(); it != mp->child.end(); ++it)
        {
            if ((*it)->key == l.first())
            {
                l.pop_front();/*先删除lable列表的第一个*/
                mp = (*it);

                if (!l.empty()) break;

                return mp->attr;
            }
        }
    }

    return m_moudle.attr;
}

QStringList EJConfig::getAttr(const QStringList &lable, const QString &subLable, const QString &attrName)
{
    QStringList ret;
    auto l = lable;
    EJModule *mp = &m_moudle;

    do
    {
        if (m_moudle.child.empty() || lable.empty() || subLable.length() == 0) break;
        while(!mp->child.empty())
        {
            for (auto it = mp->child.begin(); it != mp->child.end(); ++it)
            {
                if ((*it)->key == l.first())
                {
                    l.pop_front();
                    mp = (*it);

                    if (!l.empty()) break;

                    for (auto c = mp->child.begin(); c != mp->child.end(); c++)
                    {
                        if ((*c)->key == subLable)
                        {
                            for (auto a = (*c)->attr.begin(); a != (*c)->attr.end(); a++)
                            {
                                if (a.key() == attrName)
                                {
                                    ret << a.value();
                                }
                            }
                        }
                    }
                    return ret;
                }
            }
        }

    }while(0);

    return ret;
}

QStringList EJConfig::getValue(const QStringList &lable, const QString &subLable)
{
    QStringList ret;
    auto l = lable;
    EJModule *mp = &m_moudle;

    do
    {
        if (m_moudle.child.empty() || lable.empty() || subLable.length() == 0) break;
        while(!mp->child.empty())
        {
            for (auto it = mp->child.begin(); it != mp->child.end(); ++it)
            {
                if ((*it)->key == l.first())
                {
                    l.pop_front();/*先删除lable列表的第一个*/
                    mp = (*it);

                    if (!l.empty()) break;

                    for (auto c = mp->child.begin(); c != mp->child.end(); c++)
                    {
                        if ((*c)->key == subLable) ret << (*c)->value;
                    }
                    return ret;
                }
            }
        }

    }while(0);

    return ret;
}

bool EJConfig::addAttr(const QStringList &lable, const QString &attrName, const QString &attrValue)
{

    return false;
}

bool EJConfig::setValue(const QStringList &lable, const QStringList &subLable, const QStringList &subValue)
{

    return false;
}


void EJConfig::parse()
{
    QStack<QString>   lable;
    QStack<EJModule*> moudle;
    EJModule         *tmp = &m_moudle;
    moudle.push(&m_moudle);

    //解析XML，直到结束
    while(!m_xml.atEnd() && !m_xml.hasError())
    {
        //读取下一个element.
        QXmlStreamReader::TokenType token = m_xml.readNext();

        //如果获取的仅为StartDocument,则进行下一个
        if(token == QXmlStreamReader::StartDocument)
        {
            continue;
        }

        //EJModule *element;

        //如果获取了StartElement,则尝试读取
        if(token == QXmlStreamReader::StartElement)
        {
            //一个新的Element
            auto element = new EJModule;
            element->key = m_xml.name().toString();

            QXmlStreamAttributes attributes = m_xml.attributes();

            for (auto it = attributes.begin(); it != attributes.end(); ++it)
            {   
                element->attr.insert(it->name().toString(), it->value().toString());
            }

            token = m_xml.readNext();
            if (token == QXmlStreamReader::Characters)
            {
                element->value = m_xml.text().toString();
            }
            tmp->child.push_back(element);

            lable.push(element->key);
            moudle.push(element);
            tmp = element;
        }
        else if(token == QXmlStreamReader::EndElement)
        {
            if (lable.back() == m_xml.name())
            {
                lable.pop();
                moudle.pop();
            }
            if (moudle.empty())
            {
                tmp = &m_moudle;
            }
            else
            {
                tmp = moudle.top();
            }
        }
    }
}

void EJConfig::write()
{

}


